package com.article_main.controller;

import com.article_main.service.ArticleServiceImpl;
import com.article_protocol.pojo.dto.AddArticleDTO;
import com.article_protocol.pojo.dto.GetArticleDTO;
import com.common.cache.impl.UserCacheRepository;
import com.common.result.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@Slf4j
@Api("ArticleController")
@Validated
@RestController
@RequestMapping("v1/article/")
@CrossOrigin
public class ArticleController {

//    @ApiOperation("添加用户")
//    @RequestMapping("add")
//    public JsonResult addArticle(AddArticleDTO addArticleDTO){
//        Object data=addArticleDTO.getData();
//        return new JsonResult(ResultEnum.SECCESS,data);
//
//
//
//
//    }
    @Autowired
ArticleServiceImpl articleServiceImpl;
    @Autowired
    UserCacheRepository userCacheRepository;
    @ApiOperation("上传文章")
    @PostMapping("post")
    public JsonResult postArticle(MultipartFile multipartFile, SecurityContextHolder securityContextHolder){

//        log.info("controller:addUserDTO:${}"+addArticleDTO);
        log.info("controller开始处理上传文章请求");
        return articleServiceImpl.addArticle(multipartFile,securityContextHolder);
    }

    @GetMapping("getIndexArticles")
    public JsonResult getIndexArticles(@RequestParam(defaultValue = "1") @NotNull int pageNum){
        log.info("controller开始处理获取首页文章请求");
        List<GetArticleDTO> indexArticle = articleServiceImpl.getIndexArticle(pageNum);
        return JsonResult.ok(indexArticle);
    }
    @GetMapping("getUserArticles")
    public JsonResult getUserArticles(@RequestParam(defaultValue = "1") @NotNull int pageNum, SecurityContextHolder securityContextHolder){
        log.info("articleController打印pageNum:{}",pageNum);
        log.info("controller开始处理获取用户文章请求");
        return articleServiceImpl.getUserArticle(pageNum,securityContextHolder);
    }
    @GetMapping("getArticle")
    public JsonResult getArticle(@RequestParam long aid){
        log.info("controller开始处理获取文章请求");
        return articleServiceImpl.getArticle(aid);
    }

//    @PostMapping("upload")
//    public JsonResult uploadArticle(MultipartFile multipartFile){
//        articleService.uploadFile(multipartFile)
//        log.info("controller:file:${}"+file);
//        return new JsonResult(ResultEnum.SECCESS);
//    }

    /*
    前端富文本编辑器上传文章，
     */
    @PostMapping("post/editor")
    public JsonResult postArticleByEditor(@RequestBody AddArticleDTO addArticleDTO){
        log.info("controller:file:${}",addArticleDTO);
        log.info("Controller开始执行editor添加文章请求");
        return articleServiceImpl.postArticleByEditor(addArticleDTO);
    }

    @PostMapping("editorUploadPicture")
    public JsonResult editorUploadPicture(MultipartFile file){
        try {
            return articleServiceImpl.uploadPicture(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return JsonResult.ok();
    }

    @PostMapping("test")
    public Object test(){
        log.info("ArticleController测试dubbo远程调用");
        articleServiceImpl.test();
        return 0;
//        Object test = userCacheRepository.test();
//        return test;

    }

    @GetMapping("searchArticleByKeyword")
    public JsonResult searchArticleById(@RequestParam String keyword){
        log.info("controller开始处理搜索文章请求");
        return JsonResult.ok(articleServiceImpl.searchArticleByKeyword(keyword));
    }



}
